package com.liujit.upms.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.enums.SqlKeyword;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liujit.common.constants.ColumnConstant;
import com.liujit.common.constants.GlobalConstant;
import com.liujit.common.constants.RedisKeyConstant;
import com.liujit.common.domain.params.BaseParams;
import com.liujit.common.domain.result.DictItem;
import com.liujit.common.domain.result.PageBean;
import com.liujit.common.exception.DataNotFoundException;
import com.liujit.common.utils.DozerConvertor;
import com.liujit.common.utils.Pagination;
import com.liujit.redis.service.RedisService;
import com.liujit.upms.domain.model.SysDictItem;
import com.liujit.upms.domain.params.SysDictItemParams;
import com.liujit.upms.domain.query.SysDictItemQuery;
import com.liujit.upms.domain.result.SysDictItemResult;
import com.liujit.upms.mapper.SysDictItemMapper;
import com.liujit.upms.service.SysDictItemService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description 系统字典数据Service实现类
 * @Author liujun
 * @Date 2021-07-02 14:52:34
 */
@RequiredArgsConstructor
@Service
public class SysDictItemServiceImpl extends ServiceImpl<SysDictItemMapper, SysDictItem> implements SysDictItemService {

    private final RedisService redisService;

    @Override
    public PageBean<SysDictItemResult> getPage(SysDictItemQuery query) {
        // 排序
        if (StrUtil.isNotBlank(query.getSortName()) && StrUtil.isNotBlank(query.getSortOrder())) {
            query.setSortName(ColumnConstant.SEQ);
            query.setSortOrder(SqlKeyword.DESC.getSqlSegment());
        }
        Page<SysDictItem> page = Pagination.page(query);
        LambdaQueryWrapper<SysDictItem> wrapper = new LambdaQueryWrapper();
        if (StrUtil.isNotBlank(query.getDictCode())) {
            wrapper.like(SysDictItem::getDictCode, query.getDictCode());
        }
        if (StrUtil.isNotBlank(query.getLabel())) {
            wrapper.like(SysDictItem::getLabel, query.getLabel());
        }
        if (StrUtil.isNotBlank(query.getValue())) {
            wrapper.like(SysDictItem::getValue, query.getValue());
        }
        if (query.getEnabled() != null) {
            wrapper.eq(SysDictItem::getEnabled, query.getEnabled());
        }
        if (query.getStartTime() != null) {
            wrapper.ge(SysDictItem::getCreateTime, query.getStartTime());
        }
        if (query.getEndTime() != null) {
            wrapper.le(SysDictItem::getCreateTime, query.getEndTime());
        }
        IPage<SysDictItem> iPage = super.page(page, wrapper);
        return PageBean.page(iPage, SysDictItemResult.class);
    }

    @Override
    public Boolean save(SysDictItemParams params) {
        // 删除缓存key
        redisService.remove(RedisKeyConstant.UPMS_DICT_ITEM + params.getDictCode());
        SysDictItem entity = DozerConvertor.convertor(params, SysDictItem.class);
        // 添加
        if (params.getId() == null) {
            entity.setEnabled(GlobalConstant.ENABLED_YES);
            return super.save(entity);
        } else {
            SysDictItem sysDictItem = super.getById(params.getId());
            if (sysDictItem == null) {
                throw new DataNotFoundException();
            }
            return super.updateById(entity);
        }
    }

    @Override
    public SysDictItemResult get(Long id) {
        SysDictItem sysDictItem = super.getById(id);
        if (sysDictItem == null) {
            throw new DataNotFoundException();
        }
        return DozerConvertor.convertor(sysDictItem, SysDictItemResult.class);
    }

    @Override
    public Boolean del(BaseParams params) {
        SysDictItem sysDictItem = super.getById(params.getId());
        if (sysDictItem == null) {
            throw new DataNotFoundException();
        }
        // 删除缓存key
        redisService.remove(RedisKeyConstant.UPMS_DICT_ITEM + sysDictItem.getDictCode());
        return super.removeById(params.getId());
    }

    @Override
    public Boolean enabled(BaseParams params) {
        SysDictItem sysDictItem = super.getById(params.getId());
        if (sysDictItem == null) {
            throw new DataNotFoundException();
        }
        return super.updateById(new SysDictItem() {{
            setId(params.getId());
            setEnabled(GlobalConstant.ENABLED_YES);
        }});
    }

    @Override
    public Boolean disabled(BaseParams params) {
        SysDictItem sysDictItem = super.getById(params.getId());
        if (sysDictItem == null) {
            throw new DataNotFoundException();
        }
        // 删除缓存key
        redisService.remove(RedisKeyConstant.UPMS_DICT_ITEM + sysDictItem.getDictCode());
        return super.updateById(new SysDictItem() {{
            setId(params.getId());
            setEnabled(GlobalConstant.ENABLED_NO);
        }});
    }

    @Override
    public Integer getCountByLabel(Long id, String label) {
        LambdaQueryWrapper<SysDictItem> wrapper = new LambdaQueryWrapper();
        wrapper.eq(SysDictItem::getLabel, label);
        if (id != null) {
            wrapper.ne(SysDictItem::getId, id);
        }
        return super.count(wrapper);
    }

    @Override
    public Integer getCountByValue(Long id, String value) {
        LambdaQueryWrapper<SysDictItem> wrapper = new LambdaQueryWrapper();
        wrapper.eq(SysDictItem::getValue, value);
        if (id != null) {
            wrapper.ne(SysDictItem::getId, id);
        }
        return super.count(wrapper);
    }

    @Override
    public List<DictItem> getDictItem(String code) {
        String key = RedisKeyConstant.UPMS_DICT_ITEM + code;
        Object o = redisService.get(key);
        if (o != null) {
            return JSONUtil.toList(Convert.toStr(o), DictItem.class);
        }
        LambdaQueryWrapper<SysDictItem> wrapper = new LambdaQueryWrapper();
        wrapper.eq(SysDictItem::getDictCode, code);
        wrapper.orderByAsc(SysDictItem::getSeq);
        List<SysDictItem> list = super.list(wrapper);
        List<DictItem> dictItemList = DozerConvertor.convertor(list, DictItem.class);
        // 设置缓存
        redisService.set(key, JSONUtil.toJsonStr(dictItemList));
        return dictItemList;
    }
}
